import DataManager from "../../Runtime/DataManager"
import { IAttr, IBullte, IEnemy, IEnemySkill, IVec2, IWeapon } from "../Interface/State"
import { circle } from "../QuadTree"
import { Singleton } from "../Singleton"
import { ActorSkillTypeEnum, EntityTypeEnum, StateTypeEnum } from "../common/Enum"
import { GSEnemySKillCD } from "../common/Global"
import { getDirByPos, toFixed } from "../common/Util"
import { AttributeManager } from "./AttributeManager"
import { GameLogic } from "./GameLogic"

/** 同步射击技能数据格式 */
export type Ttbsj = { weapon: IWeapon, state: StateTypeEnum, direction: IVec2, id: number, time: number }

/** 技能管理器 */
export class SkillManager extends Singleton {

    static get Ins() {
        return super.GetInstance<SkillManager>()
    }

    /** 角色拥有的技能 */
    actorSkill = {}

    /** 怪物回血光环 */
    recoverHpHalo(enemySkill: IEnemySkill, dt: number) {

        if (enemySkill.CD > GSEnemySKillCD[enemySkill.type]) {

            const enemy = GameLogic.Ins.state.enemies.find((v) => v.id === enemySkill.owner)
            if (!enemy) {
                return
            }
            const cir: circle = { r: 100, p: enemy.position }
            const enemies = GameLogic.Ins.quadTree.retrieveByCircule(cir) as IEnemy[]
            for (const v of enemies) {
                // 恢复1%生命
                let recoverHP = Math.floor(v.hpMax / 100 + 1)
                const hp = v.hpMax - v.hp
                if (recoverHP > hp) recoverHP = hp
                v.hp += recoverHP
                v.recoverHP = recoverHP
            }
            enemySkill.CD = 0
        } else {
            enemySkill.CD += dt
        }
    }

    /** 习得技能 */
    learnSkill(entityType: number) {
        DataManager.Ins.state.skill[entityType] = {
            entityType,
            data: {}
        }
        this.actorSkill[entityType] = DataManager.Ins.state.skill[entityType]
        if (entityType == ActorSkillTypeEnum.tbsj) {
            this.actorSkill[entityType].data = {
                [EntityTypeEnum.weapon7]: {
                    weapon: DataManager.Ins.state.weapons.find(v => v.entityType == EntityTypeEnum.weapon8),
                    state: StateTypeEnum.none,
                    direction: { x: 1, y: 0 },
                    id: ++DataManager.Ins.state.nextId,
                    time: 0,
                },
                [EntityTypeEnum.weapon8]: {
                    weapon: DataManager.Ins.state.weapons.find(v => v.entityType == EntityTypeEnum.weapon7),
                    state: StateTypeEnum.none,
                    direction: { x: 1, y: 0 },
                    id: ++DataManager.Ins.state.nextId,
                    time: 0,
                },
            } as { [key: number]: Ttbsj }
        }
    }

    /** 同步射击 */
    tongBuSheJi(weapon: IWeapon, targetEnemy: IEnemy) {
        const skill = this.actorSkill[ActorSkillTypeEnum.tbsj]
        if (skill) {
            const skillData = skill.data[weapon.entityType] as Ttbsj
            if (skillData) {
                skillData.state = StateTypeEnum.shoot
                const weaponData = AttributeManager.Ins.getWeaponAttr(skillData.weapon)
                const actor = DataManager.Ins.state.actors[0]

                const x = toFixed(skillData.weapon.position.x + actor.position.x)
                const y = toFixed(skillData.weapon.position.y + actor.position.y)

                const p3 = getDirByPos(targetEnemy.position.x, targetEnemy.position.y, x, y)

                skillData.direction.x = p3.x
                skillData.direction.y = p3.y

                skillData.time = 0.2

                GameLogic.Ins.addBullet(weapon, weaponData, p3, x, y)

            }
        }
    }

    /** 经验达人 */
    jingYanDaRen() {
        const weapon1 = DataManager.Ins.state.weapons.find((v) => v.entityType === EntityTypeEnum.weapon1)
        const weapon3 = DataManager.Ins.state.weapons.find((v) => v.entityType === EntityTypeEnum.weapon3)

        if (weapon1) weapon1.shootCD *= 2
        if (weapon3) weapon3.shootCD *= 2
    }

    /** 欺软碰硬 0否 1攻击生命最高 2攻击生命最低 */
    qiRuanPengYing(weapon: IWeapon): 0 | 1 | 2 {
        const skill = this.actorSkill[ActorSkillTypeEnum.qrpy]
        if (skill) {
            if (weapon.entityType == EntityTypeEnum.weapon9) {
                return 2
            } else if (weapon.entityType == EntityTypeEnum.weapon10) {
                return 1
            }
        }
        return 0
    }

    /** 幸运子弹 */
    xingYunZiDan(bullet: IBullte) {
        const weapon = DataManager.Ins.state.weapons.find((v) => v.id === bullet.ownerWeapon)
        if (weapon.entityType == EntityTypeEnum.weapon6) {
            GameLogic.Ins.addExpBall(bullet.position)
        }
    }

    /** 火力不足恐惧症 */
    huoLiBuZuKongJuZheng(weapon: IWeapon) {
        const skill = this.actorSkill[ActorSkillTypeEnum.hlbzkjz]
        if (skill && (weapon.entityType == EntityTypeEnum.weapon2 || weapon.entityType == EntityTypeEnum.weapon4)) {
            return true
        }
        return false
    }

    /** 生命射击 */
    shengMingSheJi(weapon: IWeapon) {
        const skill = this.actorSkill[ActorSkillTypeEnum.smsj]
        if (skill && weapon.entityType == EntityTypeEnum.weapon5) {
            return true
        }
        return false
    }

    /** 站桩设计 */
    zhanZhuangSheJi(attribute: IAttr) {
        if (DataManager.Ins.state.actors[0].isMove == 0) {
            attribute.damageRate -= 0.5
        } else {
            attribute.damageRate += 1
        }
        return attribute
    }

    /** 不可一世 */
    buKeYiShi(attribute: IAttr) {
        const hp = DataManager.Ins.state.actors[0].hp
        if (hp == attribute.hpMax) {
            attribute.damageRate += 0.6
        }
        return attribute
    }

    /** 百战不殆 */
    baiZhanBuDai(attribute: IAttr) {
        const hp = DataManager.Ins.state.actors[0].hp
        const hpMax = attribute.hpMax
        let rate = 1 - (hp - hpMax * 0.2) / hpMax * 0.8
        if (rate > 1) rate = 1
        attribute.damageRate += rate * 2
        return attribute
    }

    /** 不动如山 */
    buDongRuShan(attribute: IAttr) {
        attribute.moveSpeed /= 2
        attribute.hpMaxRate += 1
        attribute.damageRate += 0.2
        return attribute
    }

    /** 神勇无畏 */
    shenYongWuWei(attribute: IAttr) {
        attribute.hpMaxRate += 0.3
        attribute.damageRate += 0.15
        attribute.defenseRate += 0.1
        return attribute
    }

    /** 贴脸射击 */
    tieLianSheJi(attribute: IAttr) {
        attribute.damageRate += 1
        attribute.range /= 2
        return attribute
    }

    /** 玻璃大炮 */
    boLiDaPao(attribute: IAttr) {
        attribute.damageRate += 1
        attribute.defense -= 100
        return attribute
    }

    reset() {
        this.actorSkill = {}
    }
}